var table;
var data;
$(function () {
    $('#table').bootstrapTable(
        {
            cache: false,
            method: 'GET',
            clickToSelect: true,
            url: 'login/index',
            showColumns: true,
            showRefresh: true,
            pagination: true,
            sidePagination: 'server',
            pageNumber: 1,
            pageSize: 5,
            pageList: [10, 25, 50, 100],
            showToggle: true,
            showPaginationSwitch: true,
            // detailView: true,
            clickToSelect: true,
            toolbar: '#toolbar',
            queryParamsType: '',
            singleSelect: true,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '编号',
                    align: 'center'
                },
                {
                    field: 'name',
                    title: '姓名',
                    align: 'center'
                }, {
                    field: 'gender',
                    title: '性别',
                    align: 'center'
                }, {
                    field: 'birthday',
                    title: '生日',
                    align: 'center'
                }, {
                    field: 'phone',
                    title: '手机号',
                    align: 'center'
                }],
            onLoadSuccess:
                function () {  //SUCCESS回调函数
                    console.log('success!');
                }

            ,
            responseHandler:
                function (res) {
                    console.log(res);
                    return {
                        "rows":
                        res.data,
                        "total":
                        res.total
                    }
                }

        })
})

function queryParams(params) {
    var temp = {
        limit: params.pageNumber,
        page: params.pageSize
    }
    return temp;

}

$('#btn-edit').click(function () {
    try {
        var selectContent = $('#table').bootstrapTable('getSelections')[0];
    } catch (err) {
        console.log(err);
    }
    console.log("after")
    if (typeof (selectContent) == 'undefined') {
        alert('请选择一项!');
    } else {
        data = {'id': selectContent.id};
        console.log(data);
        $.ajax({
            url: 'index/detail',
            type: "GET",
            success: function () {
                window.location.href = '/index/detail?id=' + selectContent.id;
            },
            error: function () {
                console.error("请求失败")
            }
        });
    }

})


$('#btn-delete').click(function(){
        var selectContent = $('#table').bootstrapTable('getSelections')[0];
    if (typeof (selectContent) == 'undefined') {
        alert('请选择一项!');
    } else {
        data = {'id': selectContent.id};
        console.log(data);
        $.ajax({
            url: 'delete',
            data:data,
            type: "GET",
            success: function () {
                window.location.href = '/';
            },
            error: function () {
                console.error("请求失败")
            }
        });
    }
})
